﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Xml.Linq;
using System.ComponentModel;
using System.Resources;
using System.Reflection;
using WikiQuizz.Common.Resource;
using System.Xml;
using WikiQuizz.Common.Utils;

namespace WikiQuizz.Common.WikiAPIHelper
{
    public class WikiAPIHelperClass
    {
        private static HttpWebRequest GetMyRequest(string URL)
        {
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);

            myRequest.UserAgent = Strings.USERAGENT;
            myRequest.Method = Strings.HTTP_METHOD;
            myRequest.Accept = Strings.HTTP_ACCEPT;

            return myRequest;
        }

        public static XElement QueryEmbeddedIn(string eiTitle)
        {            
            XElement result;

            string URL = String.Format("{0}?action=query&list=embeddedin&format=xml&eititle={1}&einamespace=0&eilimit=max", Strings.ENDPOINT, eiTitle);
            
            var myRequest = GetMyRequest(URL);
            
            using (HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse())
            {
                result = XElement.Load(response.GetResponseStream());                
            }
          
            return result;
        }

        public static XElement QueryCategorieMembers(string eiTitle)
        {
            XElement result;

            string URL = String.Format("{0}?action=query&list=categorymembers&format=xml&cmtitle={1}&cmnamespace=0&cmlimit=max", Strings.ENDPOINT, eiTitle);
            
            var myRequest = GetMyRequest(URL);
            
            using (HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse())
            {
                result = XElement.Load(response.GetResponseStream());                
            }
            
            return result;            
        }

        public static XElement QueryInfo(ICollection<int> pageIdList)
        {                        
            XElement result;

            StringBuilder pages = new StringBuilder();

            foreach (var item in pageIdList)
            {
                if (pages.Length == 0)
                {
                    pages.Append(item.ToString());
                }
                else
                {
                    pages.Append("|").Append(item.ToString());
                }
            }

            string URL = String.Format("{0}?action=query&prop=info&format=xml&pageids={1}", Strings.ENDPOINT, pages.ToString());
            
            var myRequest = GetMyRequest(URL);

            using (HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse())
            {
                result = XElement.Load(response.GetResponseStream());
            }

            return result;
        }

        public static XElement GetWikiPage(int pageId)
        {
            XElement result;

            string URL = String.Format("{0}?action=query&list=embeddedin&format=xml&eititle={1}&eilimit=max", Strings.ENDPOINT, pageId);
            
            var myRequest = GetMyRequest(URL);

            using (HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse())
            {
                result = XElement.Load(response.GetResponseStream());
            }

            return result;
        }

        public static String GetExtract(int pageId)
        {
            XElement result;
                        
            string URL = String.Format("{0}?action=query&prop=extracts&format=xml&exlimit=10&exintro=&explaintext=&pageids={1}", Strings.ENDPOINT, pageId);

            var myRequest = GetMyRequest(URL);

            using (HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse())
            {
                result = XElement.Load(response.GetResponseStream());
            }

            XElement res = (from o in result.Descendants(XName.Get("extract"))
                          select o).FirstOrDefault();


            return res.Value;
        }

        public static XElement GetCountryData(string pageName)
        {
            XElement result;

            string URL = String.Format("{0}?action=mobileview&format=xml&page={1}&sections=0&prop=text", Strings.ENDPOINT, pageName);

            var myRequest = GetMyRequest(URL);

            using (HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse())
            {
                result = XElement.Load(response.GetResponseStream());
            }

            return FormXML(result);
        }

        private static XElement FormXML(XElement item)
        {
            XElement res = (from o in item.Descendants(XName.Get("section"))
                            select o).FirstOrDefault();

            XElement formedXML = XElement.Parse(String.Format("<WikiQuizz>{0}</WikiQuizz>", res.Value));
            formedXML.Elements("p").Remove();

            return formedXML;
        }        

        public static String GetExportedPage(string pageName)
        {
            //'http://en.wikipedia.org/w/index.php?title=Special:Export&pages=Main_Page&curonly&action=submit'

            XElement result;

            string URL = String.Format("http://en.wikipedia.org/w/index.php?title=Special:Export&pages={0}&curonly&action=submit", pageName);

            var myRequest = GetMyRequest(URL);

            using (HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse())
            {
                result = XElement.Load(response.GetResponseStream());
            }

            return String.Empty;

        }

    }
}
